package com.lu.statistics.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.lu.statistics.common.config.ServerResponse;
import com.lu.statistics.common.constant.ResponseCode;
import com.lu.statistics.common.utils.LoggerUtils;
import com.lu.statistics.entity.RCustomer;
import com.lu.statistics.entity.RCustomerCart;
import com.lu.statistics.entity.enums.CheckedTypeEnum;
import com.lu.statistics.entity.front.request.CreateCustomerCart;
import com.lu.statistics.entity.front.request.UpdateCartPriceRequest;
import com.lu.statistics.entity.front.request.UpdateCartStatusRequest;
import com.lu.statistics.entity.front.response.CartProductResponse;
import com.lu.statistics.entity.front.response.CustomerCartResponse;
import com.lu.statistics.entity.vo.CartProductPrice;
import com.lu.statistics.service.CustomerCartService;
import com.lu.statistics.service.RCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(value = "购物车模块", tags = {"购物车模块"})
@RestController
@RequestMapping("/api/cart")
public class CustomerCartController {
    @Autowired
    private CustomerCartService cartService;
    @Autowired
    private RCustomerService customerService;

    @ApiOperation(value = "添加购物车", notes = "添加购物车")
    @PostMapping(value = "/addCustomerCarts")
    private ServerResponse<String> addCarts(@RequestBody CreateCustomerCart createCustomerCart) {
        ServerResponse<String> res = new ServerResponse<String>();
        try {
            RCustomerCart customerCart = new RCustomerCart();
            BeanUtils.copyProperties(createCustomerCart, customerCart);
            BigDecimal productPrice = new BigDecimal(createCustomerCart.getProductPrice());
            customerCart.setProductPrice(productPrice);
            BigDecimal price = new BigDecimal(createCustomerCart.getPrice());
            customerCart.setPrice(price);
            Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
            customerCart.setCreateId(userId);
            customerCart.setProductType(createCustomerCart.getProductType().name());
            cartService.saveOrUpdateCustomerCart(customerCart);
        } catch (Exception e) {
            LoggerUtils.error(getClass(), "[rCustomerCart addCarts]", e);
            return res.error(ResponseCode.ERROR);
        }
        return res.success();
    }

    @ApiOperation(value = "更新购物车购物信息", notes = "更新购物车购物信息")
    @PostMapping(value = "/updateCartPrice")
    private ServerResponse<String> updateCustomerCartPrice(@RequestBody UpdateCartPriceRequest request) {
        ServerResponse<String> res = new ServerResponse<String>();
        try {
            RCustomerCart customerCart = new RCustomerCart();
            BeanUtils.copyProperties(request, customerCart);
            BigDecimal productPrice = new BigDecimal(request.getProductPrice());
            customerCart.setProductPrice(productPrice);
            BigDecimal price = new BigDecimal(request.getPrice());
            customerCart.setPrice(price);
            cartService.updateCustomerCart(customerCart);
        } catch (Exception e) {
            LoggerUtils.error(getClass(), "[rCustomerCart updateCustomerCartPrice]", e);
            return res.error(ResponseCode.ERROR);
        }
        return res.success();
    }


    @ApiOperation(value = "更新购物车状态", notes = "更新购物车状态")
    @PostMapping(value = "/updateCustomerCartStatus")
    private ServerResponse<String> updateCustomerCartStatus(@RequestBody UpdateCartStatusRequest request) {
        ServerResponse<String> res = new ServerResponse<String>();
        try {
            RCustomerCart customerCart = new RCustomerCart();
            if (request.getType().name().equals(CheckedTypeEnum.NO.name())) {
                customerCart.setId(request.getId());
            }
            customerCart.setCartStatus(request.getCartStatus());
            Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
            //获取客户信息
            RCustomer customer = customerService.queryCustomerInfo(userId);
            customerCart.setCustomerId(customer.getId());
            cartService.updateCartStatus(customerCart);
        } catch (Exception e) {
            LoggerUtils.error(getClass(), "[rCustomerCart updateCustomerCartStatus]", e);
            return res.error(ResponseCode.ERROR);
        }
        return res.success();
    }

    @ApiOperation(value = "客户购物车商品信息", notes = "客户购物车商品信息")
    @GetMapping(value = "/queryCartProductInfo")
    public ServerResponse<CartProductResponse> queryCartProductInfo() {
        try {
            List<CustomerCartResponse> newList = new ArrayList<>();
            Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
            CartProductResponse productResponse = new CartProductResponse();
            CartProductPrice product = cartService.queryCartProductPrice(userId);
            BeanUtils.copyProperties(product, productResponse);
            if (product.getCartList() !=null &&product.getCartList().size() > 0) {
                for (RCustomerCart cart : product.getCartList()) {
                    CustomerCartResponse cartResponse = new CustomerCartResponse();
                    BeanUtils.copyProperties(cart, cartResponse);
                    String url = "/api/file/previewPhoto?fileFullPath=" + cart.getImgUrl();
                    boolean status = cart.getCartStatus() > 0 ? true : false;
                    cartResponse.setChecked(status);
                    cartResponse.setValue(cart.getId().toString());
                    cartResponse.setImgUrl(url);
                    newList.add(cartResponse);
                }
            }

            productResponse.setCartList(newList);
            return ServerResponse.success(productResponse);
        } catch (Exception e) {
            e.printStackTrace();
            LoggerUtils.error(getClass(), "[rCustomerCart queryCartProductInfo]" + e.getMessage());
            return ServerResponse.error();
        }
    }

    @ApiOperation(value = "客户购车列表", notes = "客户购车列表")
    @GetMapping(value = "/queryCustomerCartList")
    public ServerResponse<List<CustomerCartResponse>> queryCustomerCartList() {
        try {
            List<CustomerCartResponse> newList = new ArrayList<>();
            Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
            List<RCustomerCart> cartList = cartService.queryCustomerListByUserId(userId);
            for (RCustomerCart cart : cartList) {
                CustomerCartResponse cartResponse = new CustomerCartResponse();
                BeanUtils.copyProperties(cart, cartResponse);
                String url = "/api/file/previewPhoto?fileFullPath=" + cart.getImgUrl();
                boolean status = cart.getCartStatus() > 0 ? true : false;
                cartResponse.setChecked(status);
                cartResponse.setValue(cart.getId().toString());
                cartResponse.setImgUrl(url);
                newList.add(cartResponse);
            }
            return ServerResponse.success(newList);
        } catch (Exception e) {
            e.printStackTrace();
            LoggerUtils.error(getClass(), "[rCustomerCart queryCustomerCartList]" + e.getMessage());
            return ServerResponse.error();
        }
    }

    @ApiOperation(value = "删除购物车", notes = "删除购物车")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", required = true, name = "id", value = "购物车Id", dataType = "int")})
    @GetMapping(value = "/deleteCustomerCart")
    public ServerResponse<String> deleteCustomerCart(Integer id) {
        cartService.deleteCustomerCart(id);
        return ServerResponse.success();

    }
}
